package xyz.liuyuhe.StudySpring.ch7.cglib;

import java.lang.reflect.Method;

import org.springframework.cglib.proxy.Callback;
import org.springframework.cglib.proxy.CallbackFilter;
import org.springframework.cglib.proxy.Enhancer;

public class TaskInterceptorMain {
    public static void main(String[] args) {
        TaskInterceptorMain test = new TaskInterceptorMain();
        Task proxyTarget = (Task)test.createProxy(Task.class);
        proxyTarget.execute("123");
        proxyTarget.endTask("456");
    }
    
    @SuppressWarnings("unchecked")
    public <T> T createProxy(Class<T> targetClass){
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(targetClass);
        enhancer.setCallbacks(new Callback[]{new TaskInterceptor(),new TaskInterceptor2()});
        enhancer.setCallbackFilter(new CallbackFilter() {
            public int accept(Method arg0) {
                if(arg0.getName().equals("execute")){
                    return 0;
                }else{
                    return 1;
                }
            }
        });
        return (T)enhancer.create();
    }
}